/******************************************************
 * Cours :             TODO
 * Session :           TODO
 * Groupe :            TODO
 * Projet :            LOG610-Lab4
 * Étudiant(e)(s) :    Alexandru Marinescu
 *                     Francis Lanthier
 * Code(s) perm. :     MARB27018602
 *                     LANF17069106
 * Professeur :        TODO
 * Nom du fichier :    RequeteServeur.java
 * Date crée :         2013-03-14
 * Date dern. modif.   2013-03-14
 *******************************************************
 * Historique des modifications
 *******************************************************
 *
 *******************************************************/

package part2;

import java.net.Socket;
import java.util.Date;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class RequeteServeur implements Runnable {

	Socket socket = null;
	String fileName;

	public RequeteServeur(Socket socket) {
		this.socket = socket;
	}

	@Override
	public void run() {
		int MAXLENGTH = 512;
		byte[] buff = new byte[MAXLENGTH];
		try {
			InputStream in = socket.getInputStream();
			// On recoit le message du client
			in.read(buff);
			String message = new String(buff);

			System.out.println("Message en provenance du Client: "
					+ new String(buff));
			String[] result = message.split("\\s");

			if (result[0].equals("GET")) {
				if (result[1].startsWith("/") == true) {
					fileName = result[1].substring(1);
				} else {
					fileName = result[1];
				}
				File file = new File(fileName);
				FileInputStream inFile = new FileInputStream(fileName);
				byte[] fileInBytes = new byte[(int) file.length()];
				inFile.read(fileInBytes);
				sendToClient(new DataOutputStream(socket.getOutputStream()),
						fileInBytes, (int) file.length(), fileName);
			}
		} catch (FileNotFoundException ex) {
			// Format de header pour ereur 404 :
			// http://www.sourcestream.com/programming-stuff/java-http-server
			// A revoir
			try {
				DataOutputStream out = new DataOutputStream(
						socket.getOutputStream());

				out.writeBytes("HTTP/1.0 404 Not Found \r\n");
				out.writeBytes("Server: Java HTTP Server 1.0 \r\n");
				out.writeBytes("Date: " + new Date() + "\r\n");
				out.writeBytes("Content-Type: text/html");
				out.writeBytes("\r\n");
				out.writeBytes("<HTML>");
				out.writeBytes("<HEAD><TITLE>File Not Found</TITLE>"
						+ "</HEAD>");
				out.writeBytes("<BODY>");
				out.writeBytes("<H2>404 File Not Found: " + fileName + "</H2>");
				out.writeBytes("</BODY>");
				out.writeBytes("</HTML>");
				socket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (IOException e) {
			System.out.println(e);
		}
	}

	public void sendToClient(DataOutputStream out, byte[] message,
			int numOfBytes, String fileName) {
		try {
			out.writeBytes("HTTP/1.0 200 Document Follows\r\n");
			if (fileName.endsWith(".jpg"))
				out.writeBytes("Content-Type:image/jpeg\r\n");
			if (fileName.endsWith(".gif"))
				out.writeBytes("Content-Type:image/gif\r\n");
			if (fileName.endsWith(".png"))
				out.writeBytes("Content-Type:image/png\r\n");
			out.writeBytes("Content-Length: " + message.length + "\r\n");
			out.writeBytes("\r\n");
			out.write(message, 0, numOfBytes);
			socket.close();

		} catch (IOException e) {
			System.out.println("Erreur dans la methode sendToClient");
		}

	}

}
